package com.gzc.just.play.last.war.common.model;

/**
 * Position information
 */
public class Position {
    
    /**
     * X coordinate
     */
    private double x;
    
    /**
     * Y coordinate
     */
    private double y;
    
    /**
     * Z coordinate
     */
    private double z;
    
    /**
     * Default constructor
     */
    public Position() {
        this.x = 0.0;
        this.y = 0.0;
        this.z = 0.0;
    }
    
    /**
     * Constructor with x, y, z coordinates
     */
    public Position(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    /**
     * Constructor with x, y coordinates (z defaults to 0)
     */
    public Position(double x, double y) {
        this.x = x;
        this.y = y;
        this.z = 0.0;
    }
    
    /**
     * Calculate distance to another position
     */
    public double distanceTo(Position other) {
        double dx = this.x - other.x;
        double dy = this.y - other.y;
        double dz = this.z - other.z;
        return Math.sqrt(dx * dx + dy * dy + dz * dz);
    }
    
    /**
     * Check if within specified distance
     */
    public boolean isWithinDistance(Position other, double distance) {
        return distanceTo(other) <= distance;
    }
    
    /**
     * Get direction as string
     */
    public String getDirection() {
        // Calculate primary direction
        if (Math.abs(x) > Math.abs(y)) {
            return x > 0 ? "East" : "West";
        } else {
            return y > 0 ? "North" : "South";
        }
    }
    
    // Getters and setters
    public double getX() {
        return x;
    }
    
    public void setX(double x) {
        this.x = x;
    }
    
    public double getY() {
        return y;
    }
    
    public void setY(double y) {
        this.y = y;
    }
    
    public double getZ() {
        return z;
    }
    
    public void setZ(double z) {
        this.z = z;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        
        Position position = (Position) o;
        
        if (Double.compare(position.x, x) != 0) return false;
        if (Double.compare(position.y, y) != 0) return false;
        return Double.compare(position.z, z) == 0;
    }
    
    @Override
    public int hashCode() {
        long xBits = Double.doubleToLongBits(x);
        long yBits = Double.doubleToLongBits(y);
        long zBits = Double.doubleToLongBits(z);
        
        int result = (int) (xBits ^ (xBits >>> 32));
        result = 31 * result + (int) (yBits ^ (yBits >>> 32));
        result = 31 * result + (int) (zBits ^ (zBits >>> 32));
        return result;
    }
    
    @Override
    public String toString() {
        return String.format("(%.2f, %.2f, %.2f)", x, y, z);
    }
}